package br.com.ffm.praticas.facade;

import java.sql.SQLException;
import java.util.List;

import br.com.ffm.praticas.bean.Employer;
import br.com.ffm.praticas.dao.EmployerDAO;

public class EmployerFACADE {
	
	private static EmployerFACADE employerFACADE;
	private static EmployerDAO employerDAO;
	
	private EmployerFACADE() {
		employerDAO = new EmployerDAO();
	}
	
	/**
	 * Method that returns a unique instance of EmployerFACADE
	 * @return Returns a Instance of EmployerFACADE
	 */
	public static EmployerFACADE getInstance() {
		if (employerFACADE == null)
			employerFACADE = new EmployerFACADE();
		return employerFACADE;
	}
	
	/*EMPLOYER FACADE*/
	
	/**
	 * Method that tests if the Employer already exist
	 * @param employer Employer that will be inserted
	 * @return false if the Employer already exist
	 * @throws SQLException 
	 */
	private boolean alreadyExistEmployer(Employer employer) throws SQLException{
		if (employer != null) {
			Employer e = employerDAO.getEmployer(employer.getId());
			if (e != null)
				return true;
			else
				return false;
		}
		return false;
	}
	
	/**
	 * Method that adds a Employer
	 * @param e Employer
	 * @return false if the Employer already exist
	 * @throws SQLException 
	 */
	public boolean insert(Employer employer) throws SQLException {
		if (!alreadyExistEmployer(employer)) {
			employerDAO.insert(employer);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that removes a Employer
	 * @param e Employer
	 * @return false if the Employer don't exist
	 */
	public boolean delete(Employer employer) {
		return employerDAO.delete(employer);
	}
	
	public List<Employer> getEmployers() throws SQLException {
		return employerDAO.getEmployers();
	}
	
	/**
	 * Method that looks for a Employer
	 * @param id Employer id
	 * @return false if the Employer already exist
	 * @throws SQLException 
	 */
	public Employer getEmployer(int id) throws SQLException {
		return employerDAO.getEmployer(id);
	}
	
	/**
	 * Method that looks for a Employer
	 * @param cpf Employer CPF
	 * @return false if the Employer already exist
	 * @throws SQLException 
	 */
	public Employer getEmployer(String cpf) throws SQLException {
		return employerDAO.getEmployer(cpf);
	}
	
	/**
	 * Method that updates a Employer
	 * @param e Employer
	 * @return false if the legal Costumer don't exist
	 */
	public boolean update(Employer employer) {
		return employerDAO.update(employer);
	}
	
	/**
	 * Method that count the employers list size
	 * @return Size of employers list
	 * @throws SQLException 
	 */
	public int count() throws SQLException {
		return employerDAO.getEmployers().size();
	}

}
